<?php
	
autoLoader();

$strAction = ( true == isset( $_GET['action'] ) && true == valStr( $_GET['action'] ) ) ? $_GET['action'] : NULL;

switch( $strAction ) {
	case 'insert':
		handleInsertData();
		break;

	case 'update':
		handleUpdateData();
		break;

	case 'delete':
		handleDeleteData();
		break;

	case 'quick_insert':
		handleQuickInsertData();
		break;

	default:
		display( 'Provided action not found' );
		exit;
		break;
}
exit;

//file upload 			
function getExtension( $str ) {
	$i = strrpos( $str,"." );
	
	if( !$i ) { return ""; }
	
	$l 		= strlen( $str ) - $i;
	$ext 	= substr( $str, $i+1, $l );
	
	return $ext;
}

function getfileupload( $strnm, $strFolderPath, $pid ) {
	
	if( false == is_dir( $strFolderPath ) ) {
		mkdir( $strFolderPath );
	}
		
	$extension1 = getExtension( $_FILES[$strnm]['name'] );
	$extension1 = strtolower( $extension1 );
	
	if( ( $extension1 == "gif" ) || ( $extension1 == "jpg" ) || ($extension1 == "jpeg") || ( $extension1 == "png" ) ) {
		$a1 = $pid.rand();
		$b1 = $a1.'.'.$extension1;
		
		move_uploaded_file( $_FILES[$strnm]["tmp_name"], $strFolderPath . $b1 );
	}
	
	return $b1;
}

//end file upload

function handleInsertData() {
// 	display( $_REQUEST );
// 	display( $_POST ); exit;
	
	$strUploadDirName ="attachment/";
	
	if( false == isset( $_POST['Submit'] ) || false == valStr( $_POST['first_name'] ) ) {
		$str = '';
		$str .= '<form name=myform method=post action=modules.php?file=case_papers&message=invalid&mid=' . $_REQUEST['mid'] . '&smid=' . $_REQUEST['smid'] . '>';
		$str .= '<script language=JavaScript>document.myform.submit();</script></form>';
		echo $str;
		exit;
	}
	
	if( true == isset( $_POST['operation'] ) && 'insert' == strtolower( $_POST['operation'] ) ) {
	
		$objPatient = new BasePatients();
	
		$objPatient->setFirstName( $_POST['first_name'] );
		$objPatient->setMiddleName( ( ( true == isset( $_POST['middle_name'] )  && true == valStr( $_POST['middle_name'] ) ) ? $_POST['middle_name'] : 'NULL' ) );
		$objPatient->setLastName( $_POST['last_name'] );
		$objPatient->setMotherName( ( ( true == isset( $_POST['mother_name'] )  && true == valStr( $_POST['mother_name'] ) ) ? $_POST['mother_name'] : 'NULL' ) );
		$objPatient->setAge( ( ( true == isset( $_POST['age'] )  && true == valStr( $_POST['age'] ) ) ? $_POST['age'] : 'NULL' ) );
		$objPatient->setGender( $_POST['gender'] );
		
		if( true == valStr( $_POST['birth_date'] ) && true == valStr( $_POST['birth_date'] ) ) {
			$objPatient->setBirthDate( date( 'Y-m-d', strtotime( $_POST['birth_date'] ) ) );
		}
		
		$objPatient->setAddress( $_POST['address'] );
		$objPatient->setMaritalStatus( $_POST['marital_status'] );
		$objPatient->setOccupaction( ( ( true == isset( $_POST['occupaction'] )  && true == valStr( $_POST['occupaction'] ) ) ? $_POST['occupaction'] : 'NULL' ) );
		$objPatient->setMobileNumber( ( ( true == isset( $_POST['mobile_number'] )  && true == valStr( $_POST['mobile_number'] ) ) ? $_POST['mobile_number'] : 'NULL' ) );
		
		$objPatient->setPatientTypeId( $_POST['patient_type_id'] );
		$objPatient->setPatientAreaId( $_POST['patient_area_id'] );
		
		if( $_FILES["photo"]["name"] != "" ){
			$flcntrolnm = 'photo';
			
			$strFolderPath = $strUploadDirName . 'Profile_photo/';
			
			$rsltname 	= getfileupload( $flcntrolnm, $strFolderPath, $_POST['patient_id'] );
			$strfulpath = $strFolderPath . $rsltname;
			
			$objPatient->setPatientPhotoPath( $strfulpath );
		}
		
		if( $_FILES["idproof"]["name"] != "" ) {
			$flcntrolnm = 'idproof';
			
			$strFolderPath = $strUploadDirName . 'Id_proof/';
			
			$rsltname 	= getfileupload( $flcntrolnm, $strFolderPath, $_POST['patient_id'] );
			$strfulpath = $strFolderPath . $rsltname;
			
			$objPatient->setPatientIdProofPath( $strfulpath );
		}
		
		if( $_FILES["resproof"]["name"] != "" ) {
			$flcntrolnm = 'resproof';
			
			$strFolderPath = $strUploadDirName . 'Resident_proof/';
			
			$rsltname 	= getfileupload( $flcntrolnm, $strFolderPath, $_POST['patient_id'] );
			$strfulpath = $strFolderPath . $rsltname;
			
			$objPatient->setPatientResidentIdProofPath( $strfulpath );
		}
		
		$objPatient->setPatientId( ( ( true == isset( $_POST['referance_patient_id'] )  && true == valStr( $_POST['referance_patient_id'] ) ) ? $_POST['referance_patient_id'] : 'NULL' ) );
		$objPatient->setCreatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
		$objPatient->setUpdatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
		$objPatient->setUpdatedOn( 'now()' );
		
		if( 1 == $objPatient->insert() ) {
			handleInsertCasePaperData();
// 			$str = '';
// 			$str .= '<form name=myform method=post action=modules.php?file=view_patients&type=view&mid=' . $_REQUEST['mid'] . '&smid=' . $_REQUEST['smid'] . '>';
// 			$str .= '<script language=JavaScript>document.myform.submit();</script></form>';
// 			echo $str;
// 			exit;
		} else {
			$str = '';
			$str .= '<form name=myform method=post action=modules.php?file=case_papers&message=failed&mid=' . $_REQUEST['mid'] . '&smid=' . $_REQUEST['smid'] . '>';
			$str .= '<script language=JavaScript>document.myform.submit();</script></form>';
			echo $str;
			exit;
		}
	}
	return true;
}

function handleUpdateData() {
// 	display( $_REQUEST );
// 	display( $_POST ); exit;

	$strUploadDirName = "attachment/";
	
	if( false == isset( $_POST['Submit'] ) || false == valStr( $_POST['first_name'] ) ) {
		$str = '';
		$str .= '<form name=myform method=post action=modules.php?file=case_papers&mid=' . $_REQUEST['mid'] . '&smid=' . $_REQUEST['smid'] . '>';
		$str .= '<script language=JavaScript>document.myform.submit();</script></form>';
		echo $str;
		exit;
	}
	
	if( true == isset( $_POST['operation'] ) && 'update' == strtolower( $_POST['operation'] ) ) {
		$objPatients 	= new Patients();
		$objPatient 	= $objPatients->fetchPatientsById( $_POST['patient_id'] );
	
		$objPatient->setFirstName( $_POST['first_name'] );
		$objPatient->setMiddleName( $_POST['middle_name'] );
		$objPatient->setLastName( $_POST['last_name'] );
		$objPatient->setMotherName( $_POST['mother_name'] );
		$objPatient->setAge( $_POST['age'] );
		$objPatient->setGender( $_POST['gender'] );
		
		if( true == valStr( $_POST['birth_date'] ) && true == valStr( $_POST['birth_date'] ) ) {
			$objPatient->setBirthDate( date( 'Y-m-d', strtotime( $_POST['birth_date'] ) ) );
		}
		
		$objPatient->setAddress( $_POST['address'] );
		$objPatient->setMaritalStatus( $_POST['marital_status'] );
		$objPatient->setOccupaction( $_POST['occupaction'] );
		$objPatient->setHomeNumber( $_POST['home_number'] );
		$objPatient->setOfficeNumber( $_POST['office_number'] );
		$objPatient->setMobileNumber( $_POST['mobile_number'] );
		$objPatient->setEmailAddress( $_POST['email_address'] );
		$objPatient->setPatientTypeId( $_POST['patient_type_id'] );
		$objPatient->setPatientAreaId( $_POST['patient_area_id'] );
		$objPatient->setPatientId( ( ( true == isset( $_POST['referance_patient_id'] )  && true == valStr( $_POST['referance_patient_id'] ) ) ? $_POST['referance_patient_id'] : 'NULL' ) );
		$objPatient->setCreatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
		$objPatient->setUpdatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
		$objPatient->setUpdatedOn( 'now()' );
		
		if( $_FILES["photo"]["name"] != "" ) {
			$flcntrolnm = 'photo';
				
			$strFolderPath = $strUploadDirName . 'Profile_photo/';
				
			$rsltname 	= getfileupload( $flcntrolnm, $strFolderPath, $_POST['patient_id'] );
			$strfulpath = $strFolderPath . $rsltname;
			
			unlink( $objPatient->getPatientPhotoPath() );
			
			$objPatient->setPatientPhotoPath( $strfulpath );
		}
		
		if( $_FILES["idproof"]["name"] != "" ) {
			$flcntrolnm = 'idproof';
				
			$strFolderPath = $strUploadDirName . 'Id_proof/';
				
			$rsltname 	= getfileupload( $flcntrolnm, $strFolderPath, $_POST['patient_id'] );
			$strfulpath = $strFolderPath . $rsltname;

			unlink( $objPatient->getPatientIdProofPath() );
			
			$objPatient->setPatientIdProofPath( $strfulpath );
		}
		
		if( $_FILES["resproof"]["name"] != "" ) {
			$flcntrolnm = 'resproof';
				
			$strFolderPath = $strUploadDirName . 'Resident_proof/';
				
			$rsltname 	= getfileupload( $flcntrolnm, $strFolderPath, $_POST['patient_id'] );
			$strfulpath = $strFolderPath . $rsltname;
			
			unlink( $objPatient->getPatientResidentIdProofPath() );
			
			$objPatient->setPatientResidentIdProofPath( $strfulpath );
		}
		
		if( 1 == $objPatient->update() ) {
			$str = '';
			$str .= '<form name=myform method=post action=modules.php?file=view_patients&type=view&mid=' . $_REQUEST['mid'] . '&smid=' . $_REQUEST['smid'] . '>';
			$str .= '<script language=JavaScript>document.myform.submit();</script></form>';
			echo $str;
			exit;
		}
	}
	
	return true;
}

function handleInsertCasePaperData() {
// 	display( $_REQUEST );
// 	display( $_POST ); exit;
	
	if( false == isset( $_POST['Submit'] ) || false == valStr( $_POST['patient_id'] ) ) {
		$str = '';
		$str .= '<form name=myform method=post action=modules.php?file=case_papers&mid=' . $_REQUEST['mid'] . '&smid=' . $_REQUEST['smid'] . '>';
		$str .= '<script language=JavaScript>document.myform.submit();</script></form>';
		echo $str;
		exit;
	}
	
	if( true == isset( $_POST['operation'] ) && 'insert' == strtolower( $_POST['operation'] ) ) {
		$objCasePaper = new BaseCasePapers();
	
		$objCasePaper->setPatientId( $_POST['patient_id'] );
		$objCasePaper->setDiagnosisId( ( ( true == isset( $_POST['diagnosis_id'] ) && true == valStr( $_POST['diagnosis_id'] ) ) ? $_POST['diagnosis_id'] : 'NULL' ) );
		$objCasePaper->setChargeTypeId( ( ( true == isset( $_POST['charge_id'] ) && true == valStr( $_POST['charge_id'] ) ) ? $_POST['charge_id'] : 'NULL' ) );
		$objCasePaper->setSubmittedDate( date( 'd/m/Y' ) );
		$objCasePaper->setAmountPaid( $_POST['amount_paid'] );
		$objCasePaper->setCreatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
		$objCasePaper->setUpdatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
	
		if( 1 == $objCasePaper->insert() ) {
			$str = '';
			$str .= '<form name=myform method=post action=modules.php?file=case_papers&patient_id=' . $_POST['patient_id'] . '&mid=' . $_REQUEST['mid'] . '&smid=' . $_REQUEST['smid'] . '>';
			$str .= '<script language=JavaScript>document.myform.submit();</script></form>';
			echo $str;
			exit;
		}
	}
}

?>